package com.ruoyi.develop.orderCommissionRecord.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.IdUtils;

import javax.annotation.Resource;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.orderCommissionRule.domain.OrderCommissionRule;
import com.ruoyi.develop.orderCommissionRule.mapper.OrderCommissionRuleMapper;
import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import com.ruoyi.develop.projectBaseInfo.mapper.ProjectBaseInfoMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.develop.orderCommissionRecord.mapper.OrderCommissionRecordMapper;
import com.ruoyi.develop.orderCommissionRecord.domain.OrderCommissionRecord;
import com.ruoyi.develop.orderCommissionRecord.service.IOrderCommissionRecordService;

/**
 * 订单提成记录Service业务层处理
 *
 * @author lmm
 * @date 2024-11-14
 */
@Service
public class OrderCommissionRecordServiceImpl implements IOrderCommissionRecordService {
    @Resource
    private OrderCommissionRecordMapper orderCommissionRecordMapper;

    @Resource
    private ProjectBaseInfoMapper orderMapper;
    @Resource
    private OrderCommissionRuleMapper orderCommissionRuleMapper;



    /**
     * 查询订单提成记录
     *
     * @param id 订单提成记录主键
     * @return 订单提成记录
     */
    @Override
    public OrderCommissionRecord selectById(String id) {
        return orderCommissionRecordMapper.selectById(id);
    }

    /**
     * 查询订单提成记录
     *
     * @param orderId 订单提成记录主键
     * @return 订单提成记录
     */

    @Override
    public OrderCommissionRecord selectByOrderId(String orderId) {
        return orderCommissionRecordMapper.selectByOrderId(orderId);
    }

    /**
     * 查询订单提成记录列表
     *
     * @param entity 订单提成记录
     * @return 订单提成记录
     */
    @Override
    public List<OrderCommissionRecord> selectList(OrderCommissionRecord entity) {
        return orderCommissionRecordMapper.selectList(entity);
    }

    /**
     * 新增订单提成记录
     *
     * @param entity 订单提成记录
     * @return 结果
     */
    @Override
    public int insert(OrderCommissionRecord entity) {
        if (StringUtils.isBlank(entity.getId())) {
            entity.setId(IdUtils.fastSimpleUUID());
        }
        if(StringUtils.isNotBlank(entity.getOrderId())){
            ProjectBaseInfo order = orderMapper.selectById(entity.getOrderId());
            entity.setOrderMoney(order.getContractMoney());
            entity.setSalesmanName(order.getSalesmanName());
            entity.setSalesmanId(order.getSalesmanId());
            entity.setReturnDays(order.getReturnDays());
            order.setCommissionStatus(BusinessConstants.STATUS_YES);
            orderMapper.update(order);
        }

        if(StringUtils.isNotBlank(entity.getRuleId())){
            OrderCommissionRule rule = orderCommissionRuleMapper.selectById(entity.getRuleId());
            entity.setPercent(rule.getPercent());
//            entity.setMoney(entity.getOrderMoney().multiply(rule.getPercent()).divide(new BigDecimal(100)));
        }

        entity.setCreateBy(SecurityUtils.getUsername());
        entity.setCreateTime(DateUtils.getNowDate());
        return orderCommissionRecordMapper.insert(entity);
    }

    /**
     * 修改订单提成记录
     *
     * @param entity 订单提成记录
     * @return 结果
     */
    @Override
    public int update(OrderCommissionRecord entity) {
        entity.setUpdateBy(SecurityUtils.getUsername());
        entity.setUpdateTime(DateUtils.getNowDate());
        return orderCommissionRecordMapper.update(entity);
    }

    /**
     * 批量删除订单提成记录
     *
     * @param ids 需要删除的订单提成记录主键
     * @return 结果
     */
    @Override
    public int deleteByIds(String[] ids) {
        return orderCommissionRecordMapper.deleteByIds(ids);
    }

    /**
     * 删除订单提成记录信息
     *
     * @param id 订单提成记录主键
     * @return 结果
     */
    @Override
    public int deleteById(String id) {
        OrderCommissionRecord entity = orderCommissionRecordMapper.selectById(id);
        if(entity!=null && StringUtils.isNotBlank(entity.getOrderId())){
            ProjectBaseInfo order = orderMapper.selectById(entity.getOrderId());
            order.setCommissionStatus(BusinessConstants.STATUS_NO);
            orderMapper.update(order);
        }
        return orderCommissionRecordMapper.deleteById(id);
    }



}
